package com.cn.mobileproject.view.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cn.mobileproject.R;
import com.cn.mobileproject.bean.User;
import com.cn.mobileproject.manager.UserCacheManager;

public class SelectUserAdapter extends BaseAdapter implements OnClickListener{

	private List<User> mList = null;
	private Context mContext;
	private List<User> mSelectedList = new ArrayList<User>();
	private int module = 0;
	
	public int getModule() {
		return module;
	}
	
	public void setModule(int module) {
		this.module = module;
	}
	
	public SelectUserAdapter(Context context){
		mContext = context;
	}

	public SelectUserAdapter(Context context,List<User> selectedList){
		mContext = context;
		this.mSelectedList.clear();
		if(selectedList!=null){
			this.mSelectedList.addAll(selectedList);
		}
	}
	
	public void toggleUser(User user){
		if(mSelectedList.contains(user)){
			mSelectedList.remove(user);
		}
		else{
			mSelectedList.add(user);
		}
	}
	
	public List<User> getSelectedList() {
		return mSelectedList;
	}

	public void setSelectedList(List<User> selectedList) {
		this.mSelectedList = selectedList;
	}

	public List<User> getList() {
		return mList;
	}

	public void setList(List<User> list) {
		this.mList = list;
	}

	@Override
	public int getCount() {
		if(mList == null)	return 0;
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		User userInfo = mList.get(position);
        Holder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.select_user_item, null);
            holder = new Holder();
            holder.tvUsername = (TextView)convertView.findViewById(R.id.tvUsername);
            holder.checkBox = (CheckBox)convertView.findViewById(R.id.checkbox);
            holder.layout = (RelativeLayout)convertView.findViewById(R.id.selectuser_layout_item);
            		
            holder.checkBox.setOnClickListener(this);
            if(module==0) holder.checkBox.setVisibility(View.GONE);
            convertView.setTag(holder);
        } else
            holder = (Holder)convertView.getTag();
        userInfo = UserCacheManager.getUser(userInfo.uid);
        if(userInfo!=null){
            holder.tvUsername.setText(userInfo.username+"-"+userInfo.orgName);
            if(module!=0){
            	holder.checkBox.setTag(userInfo);
            	holder.checkBox.setFocusable(false);
            	 if(mSelectedList.contains(userInfo)){
                 	holder.checkBox.setChecked(true);
                 }
                 else{
                 	holder.checkBox.setChecked(false);
                 }
            }
            
            if(getCount()==1){
    			holder.layout.setBackgroundResource(R.drawable.preference_one_item);
    		}
    		else{
    			if(position==0){
    				holder.layout.setBackgroundResource(R.drawable.preference_first_item);
    			}
    			else if(position==getCount()-1){
    				holder.layout.setBackgroundResource(R.drawable.preference_last_item);
    			}
    			else{
    				holder.layout.setBackgroundResource(R.drawable.preference_item);
    			}
    		}
        }
        return convertView;
	}
	
	private class Holder {
        CheckBox checkBox;
        TextView tvUsername;
        RelativeLayout layout;
    }

	@Override
	public void onClick(View v) {
		if(v.getTag()!=null){
			User user =(User) v.getTag();
			if(v instanceof CheckBox){
				CheckBox checkBox = (CheckBox)v;
				if(checkBox.isChecked()){
					checkBox.setChecked(false);
					toggleUser(user);
				}
				else{
					checkBox.setChecked(true);
					toggleUser(user);
				}
				notifyDataSetChanged();
			}
		}
	}

}
